package com.dhcc.sdc.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.entity.ContraindicationItemEntity;
import com.dhcc.sdc.base.entity.ContraindicationEntity;
import com.dhcc.sdc.base.service.IContraindicationItemRuleService;
import com.dhcc.sdc.base.service.IContraindicationItemService;
import com.dhcc.sdc.base.service.IContraindicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 禁忌症条目
 *
 * @ClassName: ContraindicationItemController
 * @Description: TODO
 * @author zhangjin
 * @date 2022-09-02 15:42:37
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/contraindicationitem")
public class ContraindicationItemController extends BaseController {

	private String PREFIX = "/backend/sdc/contraindicationitem/";

	@Autowired
	private IContraindicationItemService contraindicationItemService;
    @Autowired
    private IContraindicationService contraindicationService;

    @Autowired
    private IContraindicationItemRuleService   itemRuleService;

    /**
     * 跳转到禁忌症条目列表首页
     */
    @RequestMapping("/to_index/{contraindicationId}")
    public String index(Model model,@PathVariable Long contraindicationId) {
        model.addAttribute("contraindicationId",contraindicationId);
        List<ContraindicationEntity> contraindicationlist = contraindicationService.selectList(null);
        JSONObject contraindicationMap = new JSONObject();

        contraindicationlist.stream().forEach(item->{
            contraindicationMap.put(item.getId().toString(),item.getName());
        });
        model.addAttribute("contraindicationMap",contraindicationMap);
        return PREFIX + "contraindicationitem.html";
    }

    /**
     * 跳转到新增禁忌症条目页面
     */
    @RequestMapping("/to_add/{contraindicationId}")
    public String toAdd(Model model,@PathVariable  Long contraindicationId) {
        List<ContraindicationEntity> contraindicationList = contraindicationService.selectList(null);
        Map<String ,Object> contraindicationMap = new HashMap<String ,Object>();
        contraindicationList.forEach(item->{
            contraindicationMap.put(item.getId().toString(),item.getName());
        });
        model.addAttribute("contraindicationMap",contraindicationMap);
        model.addAttribute("contraindicationId",contraindicationId);
        return PREFIX + "contraindicationitem_add.html";
    }

	/**
     * 跳转到修改禁忌症条目页面
     */
    @RequestMapping("/to_update/{contraindicationItemId}")
    public String toUpdate(@PathVariable Long contraindicationItemId, Model model) {
        ContraindicationItemEntity contraindicationItem = this.contraindicationItemService.selectById(contraindicationItemId);

        // 该条目是否有关联规则
        if (StringUtils.isEmpty(contraindicationItem.getExpression())){
            contraindicationItem.setHasRule(false);
        }else{
            contraindicationItem.setHasRule(true);
        }

        model.addAttribute("contraindicationItem",contraindicationItem);
        return PREFIX + "contraindicationitem_edit.html";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> params) {
        Page<ContraindicationItemEntity> page = new PageFactory<ContraindicationItemEntity>().defaultPage();
        List<ContraindicationItemEntity> result = this.contraindicationItemService.page(page,params);
        page.setRecords((List<ContraindicationItemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
    /**
     * 获取禁忌症条目列表
     * 地址不一样的，需要配置多个地址映射
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = {"/list/{contraindicationId}","/list"})
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params,@PathVariable(required = false) Long contraindicationId) {

        List<ContraindicationItemEntity> result=null;
        if (!params.containsKey("contraindicationId")){
            params.put("contraindicationId",contraindicationId);
        }
        result = this.contraindicationItemService.list(params);

        return result;
    }

//    /**
//     * 新增禁忌症条目
//     */
//    @RequestMapping(value = "/add")
//    @ResponseBody
//    @BussinessLog(value = "新增禁忌症条目")
//    public Object add(ContraindicationItemEntity contraindicationItem) {
//        if (CommonUtil.isOneEmpty(contraindicationItem)) {
//            throw new BizException(BizExceptionEnum.REQUEST_NULL);
//        }
//        contraindicationItem.insert();
//        return R.ok();
//    }

    /**
     * 删除禁忌症条目
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除禁忌症条目")
    public Object delete(@RequestParam Long contraindicationItemId) {
        this.contraindicationItemService.deleteById(contraindicationItemId);

        return R.ok();
    }

    /**
     * 保存禁忌症条目
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    @BussinessLog(value = "修改禁忌症条目")
    public Object save(ContraindicationItemEntity contraindicationItem) {
        if (CommonUtil.isOneEmpty(contraindicationItem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        if (contraindicationItem.getId()==null){
            contraindicationItemService.insert(contraindicationItem);
        }else {
            contraindicationItemService.updateById(contraindicationItem);
        }
        return R.ok();
    }

}
